/**
*
*  Elaborado por: Roberto Loaeza Valerio.
*                 geoAPP.
*                 28-abr-2006.
*
**/
package main;

import dbClass.Edafologia;
import dbClass.Mapas;
import dbClass.Suelos;
import ewe.data.LiveTreeNode;
import ewe.fx.Color;
import ewe.fx.IImage;
import ewe.fx.Image;
import ewe.ui.Control;
import ewe.ui.Editor;
import ewe.ui.Event;
import ewe.ui.EventListener;
import ewe.ui.Gui;
import ewe.ui.ImageControl;
import ewe.ui.MenuItem;
import ewe.ui.MessageBox;
import ewe.ui.Panel;
import ewe.ui.PenEvent;
import ewe.ui.ScrollBarPanel;
import ewe.ui.TreeControl;
import ewe.ui.TreeTableModel;
import ewe.ui.mButton;
import ewe.ui.mTabbedPanel;
import ewe.ui.mTextPad;
/**
 * Tipos de Suelo
 * @author Roberto Loaeza Valerio
 *
 */
public class TipoSuelo extends Editor{
	public Edafologia edafologia;
	public LiveTreeNode group;
	public MenuItem groupMenu = new MenuItem();	
	public TreeControl tree;
	public TreeTableModel treeModel;
	public String nSuelo="???";
	public mTabbedPanel mt;
	private  mTextPad mtp = new mTextPad(2,2);
	private Mapas data2 = new Mapas();
	
	private ImageControl imgC;
	private IImage img2;
	private boolean mostrado;
	public int color;	
	String textos[];
	String datadir=new String();
	/**
	 * Tipos de suelo
	 * @param textos Textos a mostrar
	 * @param datadir Directorio de datos
	 */
	public TipoSuelo(String textos[], String datadir) {
		this.textos = textos;
		this.datadir = datadir;
		mt = new mTabbedPanel();	
		Panel p = new Panel();
		edafologia = new Edafologia(datadir);
		
		edafologia.CargarTodo();
		if(edafologia.error.length()>1)
		new MessageBox("error",edafologia.error,MessageBox.MBOK).execute();
		p.addLast(new ScrollBarPanel(tree = new TreeControl()));
		group = edafologia.arbol;		
		treeModel = tree.getTreeTableModel();
		treeModel.hasControls = true;
		treeModel.expansionLevel = 2;
		p.add(tree);			
		tree.backGround = Color.White;
		tree.getTreeTableModel().setRootObject(group);
		
		
		mt.addItem(p, "Mapa", null).setCell(DONTSTRETCH);		
		addLast(mt);
		mtp.modify(DisplayOnly,0);
		addLast(new ScrollBarPanel(mtp)).setCell(DONTSTRETCH);
		
		Control c;
		c=addField(addNext(new mButton(textos[0])).setCell(HSTRETCH),textos[0]);
		Gui.iconize(c,new ewe.fx.mImage("ewe/opensmall.bmp",ewe.fx.Color.White),true,null);
		

		c=addField(addNext(new mButton(textos[1])).setCell(HSTRETCH),textos[1]);
		Gui.iconize(c,tick,true,null);
		
		c=addField(addNext(new mButton(textos[2])).setCell(HSTRETCH),textos[2]);
		Gui.iconize(c,cross,true,null);
		
	}
	public void action(ewe.reflect.FieldTransfer ft,Editor ed)
	{
		if (ft.fieldName.equals(textos[0])){			
			LiveTreeNode tn = (LiveTreeNode)treeModel.getTreeNodeAt(treeModel.selectedLine);
			if(tn instanceof Mapas)  {
				this.data2.copyFrom(tn);				
				img2 =new Image(datadir+data2.ruta);
				imgC = new ImageControl(img2);				
				Editor ce = new Editor();				
				ce.addLast(new ScrollBarPanel(imgC)).setControl(CENTER);								
				ce.title = "otro";
				imgC.addListener( new EventListener() {
					public void onEvent(Event ev){
						if (ev.type == PenEvent.PEN_DOWN) {
							PenEvent p = (PenEvent)ev;										
							int pixeless[] = new int[1];
							img2.getPixels(pixeless,0, p.x,p.y,1,1,1);							
							color = pixeless[0];
							Suelos suelo = edafologia.getSuelo(String.valueOf(color).toString());
							if(!suelo.aRGB.equalsIgnoreCase("-1")) {
								nSuelo = suelo.nombre;
								mtp.setText(suelo.nombre+" :"+suelo.descripcion);
							}														
						}
					}
				});			
				mt.addItem(ce,data2.pais+":"+data2.estado+":"+data2.municipio, null);				
				if(mostrado)
					mt.closeTab(1);
				mt.selectAndExpand(1);
				mt.redisplay();
				mostrado=true;
			}
			else
			{/*
				MessageBox mb = new MessageBox(T.equivalente[60],T.equivalente[40],MessageBox.MBOK);
				mb.backGround = this.backGround;
				mb.execute();*/
			}						
		}
		else if (ft.fieldName.equals(textos[1])){
			exit(1);
		}
		else if (ft.fieldName.equals(textos[2])){
			exit(0);
		}
	}
	
}
